package thread.word_matching;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Test_Main {
    public static void main(String[] args) throws UnsupportedEncodingException, ExecutionException, InterruptedException {
//        testLoad();
        testTask();
    }
    public static void testLoad() throws UnsupportedEncodingException, ExecutionException, InterruptedException {
        LoadWords loadWords = new LoadWords();
//        thread.knn.Test_Main.class.getClassLoader().getResource("bank.data").getPath();
        String path = Test_Main.class.getClassLoader().getResource("UK Advanced Cryptics Dictionary.txt").getPath();
        path = URLDecoder.decode(path, "utf-8"); //防止路径中文，统一转utf-8
        System.out.println(path);
        path = path.substring(1);
        List<String> list = loadWords.load(path);
        System.out.println(list.size());//250353


        String word = "hello";
        WordMatchingTask task = new WordMatchingTask(word, list, 0, list.size());
        FutureTask<Distance> futureTask = new FutureTask<>(task);
        Thread thread = new Thread(futureTask);
        thread.start();
        System.out.println(futureTask.get());//阻塞式方法
    }

    public static void testTask() throws UnsupportedEncodingException, ExecutionException, InterruptedException {
        LoadWords loadWords = new LoadWords();
//        thread.knn.Test_Main.class.getClassLoader().getResource("bank.data").getPath();
        String path = Test_Main.class.getClassLoader().getResource("UK Advanced Cryptics Dictionary.txt").getPath();
        path = URLDecoder.decode(path, "utf-8"); //防止路径中文，统一转utf-8
        System.out.println(path);
        path = path.substring(1);
        List<String> list = loadWords.load(path);
//        System.out.println(list.size());//250353

        Date start = new Date();
        String word = "sdt";
        Distance result = WordMatchingClassifier.classify(word, list);
        Date end = new Date();
        System.out.println(result);
        System.out.println("执行时间为："+(end.getTime() - start.getTime()));
    }
}
